package Thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ThreadDemo15 {

    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();
        Thread producer = new Thread(()->{
            int count = 0;
            while (true) {
                try {
                    blockingQueue.put(count);
                    System.out.println("生产的元素:" + count++);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        });
        Thread consumers = new Thread(()->{
            while (true) {
                try {
                    Integer result = blockingQueue.take();
                    System.out.println("消费的元素:" + result);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();
        consumers.start();
    }

}
